package com.passbook.merchant.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.passbook.common.constant.Constants;
import com.passbook.common.domain.BusinessException;
import com.passbook.common.domain.CommonErrorCode;
import com.passbook.common.util.PhoneUtil;
import com.passbook.common.util.Response;
import com.passbook.merchant.api.MerchantService;
import com.passbook.merchant.api.dto.MerchantDTO;
import com.passbook.merchant.api.dto.PassTemplateDTO;
import com.passbook.merchant.convert.MerchantConvert;
import com.passbook.merchant.entity.Merchant;
import com.passbook.merchant.mapper.MerchantMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <h1>商户服务实现类</h1>
 */

@org.apache.dubbo.config.annotation.Service
@Slf4j
public class MerchantServiceImpl implements MerchantService  {

    @Autowired
    MerchantMapper merchantMapper;

    @Autowired
    KafkaTemplate<String, String> kafkaTemplate;


    @Override
    public MerchantDTO queryMerchantById(Long merchantId) {
        Merchant merchant = merchantMapper.selectById(merchantId);
        // 使用MapStruct将entity转成DTO
        MerchantDTO merchantdto = MerchantConvert.INSTANCE.entity2dto(merchant);
        return merchantdto;
    }

    @Override
    @Transactional
    public MerchantDTO createMerchant(MerchantDTO merchantDTO) throws BusinessException {
        // 校验参数
        if (merchantDTO == null) {
            throw new BusinessException(CommonErrorCode.E_100108);
        }

        // 校验手机号
        if (StringUtils.isBlank(merchantDTO.getPhone())) {
            throw new BusinessException(CommonErrorCode.E_100112);
        }

        // 手机号格式校验
        if (!PhoneUtil.isMatches(merchantDTO.getPhone())) {
            throw new BusinessException(CommonErrorCode.E_100109);
        }

        if (StringUtils.isBlank(merchantDTO.getName())) {
            throw new BusinessException(CommonErrorCode.E_100110);
        }

        // 查询手机号码的唯一性
        Integer count = merchantMapper.selectCount(new LambdaQueryWrapper<Merchant>()
                .eq(Merchant::getPhone, merchantDTO.getPhone()));
        if (count > 0) {
            throw new BusinessException(CommonErrorCode.E_100113);
        }
        Merchant merchant = MerchantConvert.INSTANCE.dto2entity(merchantDTO);
        log.info("保存商户信息");
        merchantMapper.insert(merchant);
        return MerchantConvert.INSTANCE.entity2dto(merchant);

    }

    @Override
    public Response dropPassTemplate(PassTemplateDTO passTemplateDTO) {
        Response response = new Response();

        // 校验商户
        Long merchantId = passTemplateDTO.getId();
        if (merchantId == null) {
            response.setErrorCode(CommonErrorCode.E_100116.getCode());
            response.setErrorMsg(CommonErrorCode.E_100116.getDesc());
            return response;
        }

        Integer count= merchantMapper.selectCount(new LambdaQueryWrapper<Merchant>()
                .eq(Merchant::getId, merchantId));
        if (count == 0) {
            response.setErrorCode(CommonErrorCode.E_100104.getCode());
            response.setErrorMsg(CommonErrorCode.E_100104.getDesc());
        } else {
            // 设置优惠券过期时间
            passTemplateDTO.setStart(DateUtils.addDays(new Date(), -10));
            passTemplateDTO.setEnd(DateUtils.addDays(new Date(), 10));

            String passTemplate = JSON.toJSONString(passTemplateDTO);
            kafkaTemplate.send(
                    Constants.TEMPLATE_TOPIC,
                    Constants.TEMPLATE_TOPIC,
                    passTemplate
            );

            log.info("DropPassTemplate", passTemplate);
        }
        return response;
    }

}
